﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace HW4_d1
{

    // 订单类
    [Serializable]
    public class Order : IComparable
    {
        private int number;
        public int Number { get; set; }
        public Order()
        {
            this.Number = IndexGenerator.generate();
        }
        public Order(int index)
        {
            this.Number = index;
        }
        // 重载Equals方法
        public override bool Equals(Object obj)
        {
            Order o = obj as Order;
            return (o.Number == this.Number) && (obj != null);
        }
        // 重载GetHashCode
/*        public override int GetHashCode()
        {
            return int.Parse(this.number.ToString());
        }*/
        // 重载ToString方法
        public override string ToString()
        {
            return "订单号：" + this.Number + "\n";
        }

        public int CompareTo(object obj)
        {
            if (!(obj is Order))
            {
                throw new System.ArgumentException();
            }
            else
            {
                Order obj2 = obj as Order;
                return this.Number - obj2.Number;
            }
        }
    }
    // 订单明细类
    [Serializable]
    public class OrderDetails : IComparable
    {
        public Order index;
        private string name;
        private string customer;
        private double price;
        public string Name { get; set; }
        public string Customer { get; set; }
        public double Price { get; set; }
        // 重载Equals方法
        public override bool Equals(object obj)
        {

            OrderDetails o = obj as OrderDetails;
            bool con1 = (o.Name == this.Name);
            bool con2 = (o.Customer == this.Customer);
            bool con3 = (o.Price == this.Price);
            return (con1 && con2 && con3 && (obj != null));
        }
        // 重载GetHahCode方法
/*        public override int GetHashCode()
        {
            return int.Parse(Name) ^ int.Parse(Customer);
        }*/
        // 重载构造函数
        public OrderDetails(string[] args)
        {
            this.index = new Order();
            this.Name = args[0];
            this.Customer = args[1];
            this.Price = Double.Parse(args[2]);
        }
        public OrderDetails() { }
        // 重载ToString方法
        public override string ToString()
        {
            return "\n订单详细信息：\n" + "订单号：" + this.index.Number + "\n" + "商品名称：" + this.Name + "\n" + "客户：" + this.Customer + "\n" + "价格：" + Convert.ToString(this.Price) + "\n";
        }

        public int CompareTo(object obj)
        {
            if (!(obj is OrderDetails))
            {
                throw new System.ArgumentException();
            }
            OrderDetails d = obj as OrderDetails;
            return this.index.CompareTo(d.index);
        }
    }
    // 订单服务类
    public class OrderService
    {
        public List<OrderDetails> info = new List<OrderDetails>();
        // 去重
        private bool Duplicate(OrderDetails od)
        {
            foreach (OrderDetails o in this.info)
            {
                if (o.Equals(od))
                {
                    return false;
                }
            }
            return true;
        }
        // 添加订单
        public void addOrder(OrderDetails od)
        {
            if (od != null && this.Duplicate(od))
            {
                this.info.Add(od);
                Console.WriteLine("订单{0}添加成功", od.index.Number);
            }
            else if (od == null)
            {
                Console.WriteLine("订单为空！");
                return;
            }
            else if (!this.Duplicate(od))
            {
                Console.WriteLine("订单已存在！");
                return;
            }
            else
            {
                Console.WriteLine("未知错误！");
                return;
            }
        }
        // 删除订单
        public void delOrder(int index)
        {
            Order i = new Order(index);
            foreach (OrderDetails o in this.info)
            {
                if (i.Equals(o.index))
                {
                    this.info.Remove(o);
                    Console.WriteLine("删除成功，订单号为{0}", i.Number);
                    return;
                }
            }
            Console.WriteLine("未查询到订单{0}，删除失败！", i.Number);
            return;
        }
        // 修改订单
        public void changeOrder(int index, string[] args)
        {
            Order tmp = new Order(index);
            foreach (OrderDetails o in this.info)
            {
                if (o.index.Equals(tmp))
                {
                    this.info[o.index.Number].Name = args[0];
                    this.info[o.index.Number].Customer = args[1];
                    this.info[o.index.Number].Price = Double.Parse(args[2]);
                    Console.WriteLine("订单{0}修改完成", index);
                    return;
                }
            }
            Console.WriteLine("未查询到编号{0}的订单，请重试", index);
            return;
        }
        // 排序订单
        public void OrderSort(string model = "byNumber")
        {
            switch (model)
            {
                case "byNumber":
                    {
                        // 默认按编号排序
                        this.info.Sort();
                        break;
                    }
                case "byPrice":
                    {
                        // 使用lambda表达式自定义排序
                        this.info.Sort((o1, o2) => (o1.Price - o2.Price >= 0) ? 1 : -1);
                        break;
                    }
                default:
                    {
                        Console.WriteLine("未定义排序类型");
                        break;
                    }
            }
        }
        //Export方法
        public void Export()
        {
            XmlSerializer xmlSerializer = new XmlSerializer(this.info.GetType());
            using (FileStream fs = new FileStream(@"D:\大二下课程\C#\HW\HW5\Order.xml", FileMode.Create))
            {
                xmlSerializer.Serialize(fs, this.info);
            }
        }
        // Import方法
        public void Import(string filepath)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(this.info.GetType());
            using (FileStream fs = new FileStream(filepath, FileMode.Open))
            {
                List<OrderDetails> readInOrders = (List<OrderDetails>)xmlSerializer.Deserialize(fs);
                foreach (OrderDetails o in readInOrders)
                {
                    this.addOrder(o);
                }
            }
        }

        public void addOrder(object o)
        {
            throw new NotImplementedException();
        }
    }
    // 订单生成器，保证生成不重复的订单号
    class IndexGenerator
    {
        public static int genetatedNumber = 0;
        public static int generate()
        {
            IndexGenerator.genetatedNumber += 1;
            return IndexGenerator.genetatedNumber - 1;
        }
    }
    public class Program
    {
        static void Main(string[] args)
        {
            string[] o1 = new string[] { "联想", "曾天忱", "5000" };
            string[] o2 = new string[] { "联想", "曾天忱", "5000" };
            string[] o3 = new string[] { "华为", "曾天忱", "7500" };
            string[] o4 = new string[] { "苹果", "曾天忱", "9000" };
            string[] o5 = new string[] { "三星", "曾天忱", "6000" };
            OrderDetails oo1 = new OrderDetails(o1);
            OrderDetails oo2 = new OrderDetails(o2);
            OrderDetails oo3 = new OrderDetails(o3);
            OrderDetails oo4 = new OrderDetails(o4);
            OrderDetails oo5 = new OrderDetails(o5);
            OrderService server = new OrderService();
            server.addOrder(oo1);
            server.addOrder(oo2);
            server.addOrder(oo3);
            server.addOrder(oo4);
            server.addOrder(oo5);
            server.Export();
        }
    }
}


